package com.chuzhiyun.chengguoxy.feature.base;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewbinding.ViewBinding;

import com.chuzhiyun.chengguoxy.R;
import com.chuzhiyun.chengguoxy.databinding.ActivityBaseBinding;
import com.chuzhiyun.chengguoxy.utlis.StatusBarUtil;
import com.chuzhiyun.chengguoxy.widgets.AndroidLoadingDialog;
import com.chuzhiyun.chengguoxy.widgets.ProgressDialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import java.util.Objects;

public abstract class BaseActivity<T extends ViewBinding> extends AppCompatActivity {
    public ActivityBaseBinding baseBinding;
    public T viewBinding;
    public abstract void initView(View view);
    public abstract void initData();
    public abstract void setListener();
    public Context mContext;
    public  AndroidLoadingDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseBinding = ActivityBaseBinding.inflate(getLayoutInflater());
        setContentView(baseBinding.getRoot());
        viewBinding = getViewBinding();
        initView(baseBinding.getRoot());
        mContext = this;
        setListener();
        initData();
        setStatusBar();
        Objects.requireNonNull(this.getSupportActionBar()).hide();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    protected abstract T getViewBinding();

    public void showLoadingDialog() {
        if (loadingDialog == null) {
            loadingDialog = new AndroidLoadingDialog().setOnTouchOutside(false);
            loadingDialog.setCancelable(false);
        }
        loadingDialog.show(getFragmentManager(),"");
    }

    public void dismissLoadingDialog() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

    private void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (isUseFullScreenMode()) {
                StatusBarUtil.setStatusBarColor(this, R.color.white);
            } else {
                StatusBarUtil.setStatusBarColor(this, getStatusBarColor());
            }

            if (isUseBlackFontWithStatusBar()) {
                StatusBarUtil.setLightStatusBar(this, true, isUseFullScreenMode());
            }

        }
    }

    /**
     * 是否设置成透明状态栏，即就是全屏模式
     */
    protected boolean isUseFullScreenMode() {
        return false;
    }

    /**
     * 更改状态栏颜色，只有非全屏模式下有效
     */
    protected int getStatusBarColor() {
        return R.color.white;
    }

    /**
     * 是否改变状态栏文字颜色为黑色，默认为黑色
     */
    protected boolean isUseBlackFontWithStatusBar() {
        return true;
    }

    protected void initEditLength(EditText editText, TextView tvLengthTip, int maxLength) {
        if (editText != null) {
            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }
                @SuppressLint("SetTextI18n")
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (tvLengthTip != null) {
                        tvLengthTip.setText(s.length() + "/" + maxLength);
                    }
                }
                @Override
                public void afterTextChanged(Editable s) {

                }
            });

        }
    }
}
